package com.atguigu.jxc.service.impl;

import com.atguigu.jxc.dao.GoodsMapper;
import com.atguigu.jxc.dao.OverflowListGoodsMapper;
import com.atguigu.jxc.dao.OverflowListMapper;
import com.atguigu.jxc.dao.UserDao;
import com.atguigu.jxc.domain.ServiceVO;
import com.atguigu.jxc.domain.SuccessCode;
import com.atguigu.jxc.entity.*;
import com.atguigu.jxc.service.LogService;
import com.atguigu.jxc.service.OverflowListService;
import com.google.gson.Gson;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;

@Service
public class OverflowListServiceImpl implements OverflowListService {
    @Autowired
    OverflowListMapper overflowListMapper;

    @Autowired
    OverflowListGoodsMapper overflowListGoodsMapper;

    @Autowired
    UserDao userDao;

    @Autowired
    GoodsMapper goodsMapper;

    //@Resource
    @Autowired
    TOverflowListGoods overflowListGoods;

    @Autowired
    LogService logService;

    @Override
    public List<OverflowList> list(String sTime, String eTime) {
        return overflowListMapper.list(sTime,eTime);
    }

    /*
    * 请求URL：http://localhost:8080/customerReturnListGoods/save?returnNumber=XT1605772786495（退货单号）
        请求参数：CustomerReturnList customerReturnList, String customerReturnListGoodsStr
        请求方式：POST
        返回值类型：JSON
        返回值：ServiceVO
    * */
    //save用商品报溢的保存
    @Override
    public ServiceVO save(OverflowList overflowList, String overflowListGoodsStr) {
        Gson gson = new Gson();
        List<TOverflowListGoods> overflowListGoodsList = gson.fromJson(overflowListGoodsStr, new com.google.gson.reflect.TypeToken<List<TOverflowListGoods>>(){}.getType());
        //用户
        User user = userDao.findUserByName((String) SecurityUtils.getSubject().getPrincipal());
        overflowList.setUserId(user.getUserId());

        //保存商品溢单
        overflowListGoodsMapper.saveOverflowList(overflowList);

        //保存商品溢单商品详细情况
        for (TOverflowListGoods overflowListGoods : overflowListGoodsList) {
            //保存id
            overflowListGoods.setOverflowListId(overflowList.getOverflowListId());
            //保存详情
            overflowListGoodsMapper.saveOverflowListGoods(overflowListGoods);
        }

        return new ServiceVO<>(SuccessCode.SUCCESS_CODE,SuccessCode.SUCCESS_MESS);
    }


}
